import type {AxiosResponse} from 'axios'
import Request from '@/service/req'
import {TOKEN, covertCDN} from './user-info'

// 请求结果
const GITHUB_STATUS = {
  200: '请求成功',
  201: '创建成功',
  401: '认证失败！请输入有效token',
  403: '禁止访问，token可能失效！',
  404: '请求路径未找到',
  409: '资源已存在',
  500: '服务不可用',
  503: '服务不可用',
}

const request = new Request({
  baseURL: 'https://api.github.com',
  timeout: 1000 * 60 * 5,
  headers: {
    'Authorization': `token ${TOKEN}`,
  },
  interceptors: {
    // 请求拦截器
    requestInterceptors: config => config,


    // 错误请求
    requestInterceptorsCatch(err) {
      console.log('requestInterceptorsCatch', err)
    },

    // 响应拦截器实现
    responseInterceptors: (res: AxiosResponse): AxiosResponse => {
      console.log('responseInterceptors', res);
      const {status, data: {content: {path, name}}} = res
      const file = ''
      if (status == 201) {
        res.data = {...res.data, filePath: covertCDN(path), fileName: name}
      }
      // return null
      return res
    },

    // 响应拦截器错误处理
    responseInterceptorsCatch: function (error): any {

      console.log('responseInterceptorsCatch', error);

      return error
    }

  },
})


export default request
